package com.example.excel;

import com.example.excel.moudle.TleEntIllegalAnalysisResult;
import com.example.excel.moudle.TleFenceFzjyAnalysisV2Result;
import com.example.excel.moudle.TleTaxiVehicleInOutCheckData;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Hellow
 */
public class Test2 {


    public static void main(String[] args) {

        List<TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData>> dataList = new Test2().getList();

        // 二级标题栏
        String[] headers = {"序号", "车牌号", "车牌颜色", "行业", "车辆营运状态", "所属企业", "绕圈次数", "绕圈时长", "最后一次绕圈地点", "最后一次绕圈时间", "是否重点监管"};


        //list写入Excel文件
        String filePath = "C:\\Users\\Hellow\\Desktop\\kafka\\src\\main\\resources\\新建 XLS 工作表2.xls";
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Vehicle Data");

            int rowNum = 0;
            for (TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData> moudle : dataList) {



                rowNum = setHeader(workbook, sheet, rowNum, headers, "dfwsfs");

                // 写入数据
                int orderId = 1;
                for (TleTaxiVehicleInOutCheckData data : moudle.getList()) {
                    Row row = sheet.createRow(rowNum);
                    row.createCell(0).setCellValue(orderId++);
                    row.createCell(1).setCellValue(data.getPlateNo());
                    row.createCell(2).setCellValue(data.getPlateColorName());
                    row.createCell(3).setCellValue(data.getIndustry());
                    row.createCell(4).setCellValue(data.getOperStatusName());
                    row.createCell(5).setCellValue(data.getVehEntName());
                    row.createCell(6).setCellValue(data.getAbnormalNum());
                    row.createCell(7).setCellValue(data.getCountAwaitTime());
                    row.createCell(8).setCellValue(data.getLastIllegalFence());
                    row.createCell(9).setCellValue(data.getLastIllegalTime());
                    row.createCell(10).setCellValue(data.getEmphasisStatusName());
                    rowNum++;
                }
            }




            // 调整列宽
            for (int i = 0; i < headers.length; i++) {
                sheet.autoSizeColumn(i);
            }

            // 写入文件
            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
                workbook.write(outputStream);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    private static int setHeader(Workbook workbook, Sheet sheet, int rowNum, String[] headers, String firstHeaderContent) {
        // 设置一级标题行样式
        CellStyle firstHeaderStyle = workbook.createCellStyle();
        firstHeaderStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        firstHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        firstHeaderStyle.setAlignment(HorizontalAlignment.CENTER);


        // 设置二级标题行样式
        CellStyle secondHeaderStyle = workbook.createCellStyle();
        secondHeaderStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        secondHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        secondHeaderStyle.setAlignment(HorizontalAlignment.CENTER);


        // 设置一级标题行
        Row firstHeaderRow = sheet.createRow(rowNum);
        sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 10));
        Cell cell1 = firstHeaderRow.createCell(0);
        cell1.setCellValue(firstHeaderContent);
        cell1.setCellStyle(firstHeaderStyle);
        rowNum++;


        // 设置二级标题行
        Row secondHeaderRow = sheet.createRow(rowNum);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = secondHeaderRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(secondHeaderStyle);
        }
        rowNum++;
        return rowNum;
    }

    private List<TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData>> getList() {

            // 创建 TleTaxiVehicleInOutCheckData 对象列表
            List<TleTaxiVehicleInOutCheckData> dataList = new ArrayList<>();


        // Create and add multiple objects to the list
        dataList.add(new TleTaxiVehicleInOutCheckData(1, "ABC123", "Blue", "巡游出租", "Operating", "Company A", 2, 3600L, "Location 1", "2024-03-14", "Yes"));
        dataList.add(new TleTaxiVehicleInOutCheckData(2, "XYZ456", "Red", "巡游出租", "Idle", "Company B", 0, 0L, "Location 2", "2024-03-13", "No"));

            // 假设这里添加了一些 TleTaxiVehicleInOutCheckData 对象到 dataList

            // 创建 TleEntIllegalAnalysisResult 对象并存储到列表
            List<TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData>> resultList = new ArrayList<>();

            // 模拟创建几个 TleEntIllegalAnalysisResult 对象
            TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData> result1 = new TleEntIllegalAnalysisResult<>();
            result1.setVehEntId("1");
            result1.setVehEntName("企业1");
            result1.setOperStatusCode("1001");
            result1.setOperStatusName("营运中");
            result1.setEntRegionName("Region1");
            result1.setEntRegionCode("001");
            result1.setVehNum(10);
            result1.setVehIllegalNum(5);
            result1.setList(dataList);
            resultList.add(result1);

            TleEntIllegalAnalysisResult<TleTaxiVehicleInOutCheckData> result2 = new TleEntIllegalAnalysisResult<>();
            result2.setVehEntId("2");
            result2.setVehEntName("企业2");
            result2.setOperStatusCode("1002");
            result2.setOperStatusName("停运");
            result2.setEntRegionName("Region2");
            result2.setEntRegionCode("002");
            result2.setVehNum(8);
            result2.setVehIllegalNum(3);
            result2.setList(dataList);
            resultList.add(result2);

            return  resultList;


    }
}
